﻿/* File Created: 六月 15, 2012 */
// 绑定事件
function BindEvent(element, eventName, fun) {
    if (typeof element === "string") {
        element = document.getElementById(element);
    }
    if (document.attachEvent) {
        element.attachEvent("on" + eventName, fun);
    }
    else {
        element.addEventListener(eventName, fun, false);
    }
}

//function AddEventListener(element, eventName, fun) {
//    if (window.attachEvent) {
//        element.attachEvent("on" + eventName, fun);
//    }
//    else {
//        element.addEventListener(eventName, fun, false);
//    }
//}

//监听事件(flag可选参数)
function AddEventListener(element, eventName, fun, flag) {
    if (flag) {
        window.attachEvent ? element.attachEvent('on' + eventName, fun) : element.addEventListener(eventName, fun, true); //事件冒泡
    }
    else {
        window.attachEvent ? element.attachEvent('on' + eventName, fun) : element.addEventListener(eventName, fun, false); //事件捕获
    }
}

//事件模拟
//function TriggerClick(element) {
//    if (element.click) {
//        element.fireEvent("onclick");    // element.click();
//    } else {
//        try {
//            var event = document.createEvent('Event'); //HTMLEvents
//            event.initEvent('click', true, true); //事件冒泡
//            element.dispatchEvent(event);
//        }
//        catch (e) {
//            alert(e)
//        };
//    }
//}

//响应事件模拟
function TriggerClick(element) {
    if (window.attachEvent) {
        element.fireEvent("onclick");    // element.click();
    } else {
        try {
            var event = document.createEvent('Event'); //HTMLEvents
            event.initEvent('click', true, true); //事件冒泡
            element.dispatchEvent(event);
        }
        catch (e) {
            alert(e)
        };
    }
}


//事件模拟(MouseEvents)
function SimulateClick(element, eventName) {
    if (window.attachEvent) {
        element.fireEvent("on" + eventName);    // element.click();
    }
    else {
        var evt = document.createEvent("MouseEvents");
        evt.initMouseEvent(eventName, true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
        document.getElementById(element).dispatchEvent(evt);
    }
}

//响应键盘回车事件
function EnterSubmit(formName) {
    $("#" + formName).bind("keyup", function (event) {
        if (event.keyCode === "13") {
            $("#" + formName).submit(); //Ajax
        }
    });
}

// 得到输入
function GetCode(e) {
    var charCode = null;
    if (window.event) {
        charCode = window.event.keyCode;
    }
    else {
        charCode = e.which;
    }
    return charCode;
}

// 获取变量数据类型 
function GetType(obj) {
    return (obj === null) ? "null" : typeof (obj);
}

//根据元素id获得元素
function GetID(elementName) {
    return document.getElementById(elementName);
}

// 获得事件源对象
function GetEventSrcElement(e) {
    if (window.event) {
        return window.event.srcElement;
    }
    else {
        return e.target; //e.currentTarget
    }
}

// 获得事件源对象 event IE9 修正
function GetEventElement(e) {
    return (e || event).srcElement || (e || event).target;
}

// 取消事件
function CancelEvent(e) {
    if (window.event) {
        // IE中取消事件
        window.event.returnValue = false;
    }
    else {
        // w3c ff 取消事件
        e.preventDefault(); //在火狐文档
    }
}

// 取消冒泡
function CancelBubble(e) {
    if (typeof event !== 'undefined') {// event IE
        //if (window.event) {// event IE
        window.event.cancelBubble = true;
    }
    else { // e w3c
        //alert(e.stopPropagation);
        e.stopPropagation();
    }
}


//数值排序
function SortNumByAsc(a, b) {
    return a - b;
}
function SortNumByDesc(a, b) {
    return b - a;
}
//单键排序
function SortByTagName(name) {
    return function (o, p) {
        var a, b;
        if (typeof o === "object" && typeof p === "object" && o && p) {
            a = o[name];
            b = p[name];
            if (a === b) {
                return 0;
            }
            if (typeof a === typeof b) {
                return a < b ? -1 : 1;
            }
            return typeof a < typeof b ? -1 : 1;
        }
        else {
            throw ("error");
        }
    }
}
//多键排序 (def可选参数)
function SortByTagName(name, def) {
    return function (o, p) {
        var a, b;
        if (o && p && typeof o === 'object' && typeof p === 'object') {
            a = o[name];
            b = p[name];
            if (a === b) {
                return typeof def === 'function' ? def(o, p) : 0;
            }
            if (typeof a === typeof b) {
                return a < b ? -1 : 1;
            }
            return typeof a < typeof b ? -1 : 1;
        } else {
            throw ("error");
        }
    }
}